#include<stdio.h>
#include"struct.h"
#include<string.h>
#include<stdlib.h>

//Check function in all
int CheckAll(pdatatype NowPoint,pdatatype head){
	if(Check12char(NowPoint)==-1||Checkifcan(NowPoint)==-1||\
					Checkmonth(NowPoint)==-1||Checksamenumber(NowPoint,head)==-1)
		return -1;
}

//判断能否计算出失效日期
int Checkifcan(pdatatype NowPoint){
	//		加权
	int ch1=1*(!(NowPoint->date.shengchan[0]=='#'));
	int ch3=3*(!(NowPoint->date.youxiaoqi[0]=='#'));
	int ch4=4*(!(NowPoint->date.shixiao[0]=='#'));
	ch1=ch1+ch3+ch4;
	if (ch1<4) {
		printf("ERROR 输入的条件无法算出失效期\n");
		return -1;
	}
	else
		return 0;
}
//检测编号是否为十二位
int Check12char(pdatatype NowPoint){
	//Check12char
	char* pcheck=&NowPoint->Number[0];
	int i=0;
	for(;i!=12;i++,pcheck++){

		if(!(((*pcheck>='0')&&(*pcheck<='9'))||((*pcheck>='a')&&(*pcheck<='z'))))
		{
			printf("ERROR!Number is 12 char not %d char\n%s\n",i,NowPoint->Number);
			return -1;
		}
	};
	return 0;

}
//检测有效月份
int Checkmonth(pdatatype NowPoint){
	//Check month
	char *pcheck=&(NowPoint->date.youxiaoqi[0]);
	int i=0;
	for(;(*pcheck>='0'&&*pcheck<='9');i++,pcheck++);
	if(i==2)
		return 0;
	printf("ERROR!月份是整数且只能输入两位\n");
	return -1;

}
//检测是否有相同编号
int Checksamenumber(pdatatype NowPoint, pdatatype Head){
	//Check the same number
	for(pdatatype Phead=Head;Phead!=NULL&&Phead->Number\
		==NowPoint->Number;Phead=Phead->Next){
		if(Phead!=NowPoint){
			printf("ERROR! The number has been used.Try other Number:%s\n",\
				   NowPoint->Number);
			return -1;
		}

	}
	return 0;
}

//Check func end



int printsoon(pdatatype head){
	if(head==NULL)
	{
		printf("ERROR!No Data!\n");
		return -1;
	}
	compute(head);
	pdatatype poin=head;
	for(;poin!=NULL;poin=poin->Next)
	{
		if(poin->date.soonguoqi==1){
			printf("名称:%s\t 编号:%s\t 失效日期:%s\t 主治类型:%s\t\n",\
				   poin->name,poin->Number,\
				   poin->date.shixiao,poin->CureType);
		}
	};
	return 0;

}

int printguoqi(pdatatype head){
	if(head==NULL)
	{
		printf("ERROR!No Data!\n");
		return -1;
	}
	compute(head);
	pdatatype poin=head;
	for(;poin->Next!=NULL;poin=poin->Next)
	{
		if(poin->date.guoqi==1){
			printf("名称:%s\t 编号:%s\t 失效日期:%s\t 主治类型:%s\t\n",\
				   poin->name,poin->Number,\
				   poin->date.shixiao,poin->CureType);
		}
	};
	return 0;
}

////OKOK
int fileinput(pdatatype head){
	extern pdatatype fileinputpoint;
	pdatatype NowPoint;
	extern int init;
	if(init==1){
		init=0;
		NowPoint=head;
	}
	else
		NowPoint=fileinputpoint;

	//make sure this is the end
	//	if(==NULL)
	printf("请输入\t0:文本录入\t1:手工录入\n");
	extern int record;
	int test;
	scanf("%d",&test);

	if(test==0){
		printf("确保DATA文档内一行一个药品信息且信息以空格隔开没有内容的以‘#’号代替");
		printf("信息以空格隔开没有内容的以‘#’号代替且以‘×’结束\n含义:序号\t名称"\
			   "\t 编号\t\t 生产日期\t 有效期(月份)\t 失效日期\t 治疗类型\t 用法"\
			   "\t 一日服用次数\t 一次服用量\n例如:0\t阿司匹林\t abc123456789"\
			   "\t 20150101\t 24\t #\t\t 感冒药\t\t 口服\t 2-3\t\t 1-2\n*"\
			   "\n确定?(YES/NO)\n");
		char dece[4];
		scanf("%s",dece);
		//Text input
		if(strcmp(dece,"YES")!=0)
		{
			return -1;
		}
		else
		{
			FILE* pfile;
			pfile=fopen("DATA","r");

			fscanf(pfile,"%s %s %s %s %s %s %s %s %s %s",\
				   NowPoint->Num,\
				   NowPoint->name,NowPoint->Number,NowPoint->date.shengchan,\
				   NowPoint->date.youxiaoqi,NowPoint->date.shixiao,\
				   NowPoint->CureType,NowPoint->usage.UseType,\
				   NowPoint->usage.OneDay,NowPoint->usage.OneTime);

			while(NowPoint->Num[0]!='*'){
				//Check
				if(Check12char(NowPoint)==-1){

					return -1;
				}
				if(Checkifcan(NowPoint)==-1){

					return -1;
				}
				if(NowPoint->date.youxiaoqi[0]!='#')
				{
					if(Checkmonth(NowPoint)==-1){

						return -1;
					}
				}
				record++;
				if(Checksamenumber(NowPoint,head)==-1);
				NowPoint->Next=(pdatatype)malloc(STRUCTSIZE);
				NowPoint=NowPoint->Next;
				fscanf(pfile,"%s %s %s %s %s %s %s %s %s %s",\
					   NowPoint->Num,\
					   NowPoint->name,NowPoint->Number,NowPoint->date.shengchan,\
					   NowPoint->date.youxiaoqi,NowPoint->date.shixiao,\
					   NowPoint->CureType,NowPoint->usage.UseType,\
					   NowPoint->usage.OneDay,NowPoint->usage.OneTime);

			}

			fileinputpoint=NowPoint;
			NowPoint->Next=NULL;
			printf("sucess!\n");
			fclose(pfile);
		}
		//manual input

	}
	else if(test==1)
	{
		extern pdatatype Head;
		do{
			sprintf(NowPoint->Num,"%d",record);
			//		printf("%s,%d",NowPoint->Num,record);
			printf("name\n");scanf("%s",NowPoint->name);
			printf("12 Number\n");scanf("%s",NowPoint->Number);
			printf("shengchan\n");scanf("%s",NowPoint->date.shengchan);
			printf("youxiaoqi\n");scanf("%s",NowPoint->date.youxiaoqi);
			printf("shixiao \n");scanf("%s",NowPoint->date.shixiao);
			printf("CureType(感冒药、胃药、消炎药、滴眼液)\n");scanf("%s",NowPoint->CureType);
			printf("UseType\n");scanf("%s",NowPoint->usage.UseType);
			printf("OneDay\n");scanf("%s",NowPoint->usage.OneDay);
			printf("OneTime\n");scanf("%s",NowPoint->usage.OneTime);
		}
		while(CheckAll(NowPoint,Head)==-1);

		NowPoint->Next=(pdatatype)malloc(STRUCTSIZE);
		fileinputpoint=NowPoint->Next;
		fileinputpoint->Next=NULL;
		//		NowPoint->Next=malloc(STRUCTSIZE);
		//		NowPoint=NowPoint->Next;
		record++;
		printf("OK!\n");
	}
	else
	{
		printf("WRONG NUMBER!\n");
		return -1;
	}
	return 0;
}
////

int fedit(pdatatype head, pdatatype fronhead){
	if(head->Next!=NULL){
		printf("find it!\n0 to delete it or 1 to modify\n");
		short c;
		scanf("%d",&c);
		if(c==0){
			extern pdatatype Head;
			//			pdatatype tmp;
			if(head==fronhead)
			{
				pdatatype tmp=Head;
				Head=Head->Next;
				free(tmp);
			}else
			{
				pdatatype tmp=head->Next;
				free(fronhead->Next);
				fronhead->Next=tmp;

			}
			printf("OK");
		}
		else
		{
			getchar(); //清除输入流中的回车
			while(1){
				printf("name('Enter'to leave it what like\nPrevious:%s\n",head->name);
				char tmpchar[20];
				gets(tmpchar);
				if((tmpchar[0]!=0)){
					strcpy(head->name,tmpchar);
				}
				printf("12 Number(the same)\nPrevious:%s\n",head->Number);
				gets(tmpchar);
				if((tmpchar[0]!=0)){
					strcpy(head->Number,tmpchar);
				}

				printf("shengchan (if don't have ,keep it '#')\nPrevious:%s\n",head->date.shengchan);
				gets(tmpchar);
				if((tmpchar[0]!=0)){
					strcpy(head->date.shengchan,tmpchar);
				}
				printf("youxiaoqi (if don't have ,keep it '#')\n:Previous:%s\n",head->date.youxiaoqi);
				gets(tmpchar);
				if((tmpchar[0]!=0)){
					strcpy(head->date.youxiaoqi,tmpchar);
				}
				printf("shixiao (if don't have ,keep it '#')\nPrevious:%s\n",head->date.shixiao);
				gets(tmpchar);
				if((tmpchar[0]!=0)){
					strcpy(head->date.shixiao,tmpchar);
				}
				printf("CureType(感冒药、胃药、消炎药、滴眼液)\nPrevious:%s\n",head->CureType);
				gets(tmpchar);
				if((tmpchar[0]!=0)){
					strcpy(head->CureType,tmpchar);
				}
				printf("UseType\nPrevious:%s\n",head->usage.UseType);
				gets(tmpchar);
				if((tmpchar[0]!=0)){
					strcpy(head->usage.UseType,tmpchar);
				}
				printf("OneDay\nPrevious:%s\n",head->usage.OneDay);
				gets(tmpchar);
				if((tmpchar[0]!=0)){
					strcpy(head->usage.OneDay,tmpchar);
				}
				printf("OneTime\nPrevious:%s\n",head->usage.OneTime);
				gets(tmpchar);
				if((tmpchar[0]!=0)){
					strcpy(head->usage.OneTime,tmpchar);
				}
				extern pdatatype Head;
				if(CheckAll(head,Head)!=-1){
					break;
				}

			}
		}
		return 0;
	}
	else
		return -1;
}

int editinfo(pdatatype head){
	printf("0:按名称查找\t1：按编号查找\n");
	int a;
	scanf("%s",&a);
	a=a-'0';
	//	printf("%d",a);
	if(a==0){
		char b[20];
		printf("input name\n");
		scanf("%s",b);
		//		printf("%s",b);
		pdatatype fronhead=head;
		int x=strcmp(head->name,b)==0;
		int n=head->Next==NULL;
		while(1)
		{
			x=strcmp(head->name,b)==0;
			n=head->Next==NULL;
			if(x||n)
			{

				if(fedit(head,fronhead)== -1)
				{
					printf("don't find!\n");
					return -1;
				}
				else
					return 0;
			}
			head=(fronhead=head)->Next;
		}
	}
	else if(a==1)
	{
		char c [20];
		printf("input number\n");
		scanf("%s",c);
		pdatatype fronhead=head;
		for(;strcmp(head->Number,c)||head==NULL;head=(fronhead=head)->Next){
			if(fedit(head,fronhead)== -1)
			{
				printf("don't find!\n");
				return -1;
			}
		}
	}
	else
	{
		printf("wrong number!\n");
		return -1;
	}
	//	return 0;

}
//OK
void funcexit(){
	exit(0);
}

int funcdelete(pdatatype head){
	editinfo(head);
	return 0;
}
//OK
void funcsave(pdatatype head){
	FILE *pfile;
	pfile=fopen("DATA","w");
	for(;head->Next!=NULL;head=head->Next){
		fprintf(pfile,"%s %s %s %s %s %s %s %s %s %s\n",head->Num,head->name,head->Number,\
				head->date.shengchan,head->date.youxiaoqi,head->date.shixiao,\
				head->CureType,head->usage.UseType,head->usage.OneDay,\
				head->usage.OneTime);
	}
	fprintf(pfile,"%s","*");
	fclose(pfile);
	printf("complete!\n");


}

